﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Game.System.EventSystem.EventWindow
{
    /// <summary>
    /// 事件节点基类
    /// </summary>
    public abstract class BaseEventNode
    {
        // 节点位置和大小
        public Rect nodeRect = new Rect(10, 10, 250, 100);
        // 节点标题
        public string nodeTitle;
        // 事件名
        public string eventName;
        // 事件ID
        public int id;
        // 节点是否合法
        public bool legal;
        // 节点颜色
        protected Color m_TextColor;
        // 父结点
        public EventGroupNode parent;

        // 绘制节点
        public virtual void Draw()
        {
            // 复制一个style
            GUIStyle style = new GUIStyle(GUI.skin.window);
            // 仅修改文字颜色
            style.normal.textColor = m_TextColor;
            // 使用GUI.Window绘制节点
            nodeRect = GUI.Window(id, nodeRect, NodeCallback, nodeTitle, style);
        }

        /// <summary>
        /// 节点回调函数
        /// </summary>
        /// <param name="id"></param>
        protected virtual void NodeCallback(int id)
        {

            //绘制Name输入框
            GUI.Label(new Rect(15, 25, 80, 20), "名字");
            eventName = GUI.TextField(new Rect(70, 25, 160, 20), eventName);
            
            if (id == 0)
            {
                GUI.Label(new Rect(15, 55, 200, 20), "根事件组");
                legal = true; //woc
            }
            else
            {
                legal = false;
                if (string.IsNullOrEmpty(eventName))
                {
                    GUI.Label(new Rect(15, 55, 200, 20), "请输入事件名");
                }
                else if (eventName == "Root")
                {
                    GUI.Label(new Rect(15, 55, 200, 20), "不可命名为Root！");
                }
                else if (EventGranphView.IsEventNameRepeated(eventName, id))
                {
                    GUI.Label(new Rect(15, 55, 200, 20), "事件名重复！");
                }
                else
                {
                    legal = true;
                }
            }

            // 使节点可以拖动,2021版要写到函数末尾才能编辑输入框
            GUI.DragWindow();
        }

        public virtual bool ClickOn(Vector2 pos, out BaseEventNode node)
        {
            if (nodeRect.Contains(pos))
            {
                node = this;
                return true;
            }
            node = null;
            return false;
        }

        //删除节点
        public virtual void DeleteNode()
        {
            if (parent != null)
            {
                parent.DeleteNode(this);
            }
        }

        public virtual bool IsNameRepeated(string name, int id)
        {
            if (eventName == name && id != this.id)
            {
                return true;
            }
            return false;
        }

        public virtual void MoveNode(Vector2 delta)
        {
            nodeRect.position += delta;
        }

        public abstract void CopyNode(List<SerializableNode> container);

        public virtual void LoadNode(List<SerializableNode> container, int id)
        {
            var data = container.Find(n => n.id == id);
            if (data != null)
            {
                eventName = data.name;
                this.id = data.id;
                nodeRect = data.rect;
            }
        }
    }
}
